﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TheLogWatcher
{
    public class LogFormat
    {
        
        /// <summary>
        ///     Name of the log format, used as internal identifier.
        /// </summary>
        public string Name
        {
            set { this.name = value; }
            get { return this.name; }
        }
        private string name;

        
        /// <summary>
        ///     The regex of the full log format.
        /// </summary>
        public string RegEx
        {
            get
            {
                if (this.regEx == null)
                {
                    StringBuilder builder = null;
                    if (this.parts != null)
                    {
                        builder = new StringBuilder();
                        for (int i = 0; i < this.parts.Count; i++)
                        {
                            builder.Append(this.parts[i].PreText);
                            builder.Append("(?<" + this.parts[i].Title + ">");
                            builder.Append(this.parts[i].RegEx + ")");
                        }
                    }
                    this.regEx = builder.ToString();
                }
                return this.regEx;
            }
        }
        private string regEx;

        /// <summary>
        ///     All parts of this log format
        /// </summary>
        public List<Part> Parts
        {
            set { this.parts = value; }
            get { return this.parts; }
        }
        private List<Part> parts;


        public void addPart(string title, int standardWidth, string preText, string regex, string format)
        {
            if (title != null)
            {
                Part part = new Part();
                part.Title = title;
                part.StandardWidth = standardWidth;
                part.PreText = preText;
                part.RegEx = regex;
                part.Format = format;
                if (this.parts == null)
                {
                    this.parts = new List<Part>();
                }
                this.parts.Add(part);
            }
        }

        public void addPart(string title, int standardWidth, string preText, string regex)
        {
            addPart(title, standardWidth, preText, regex, null);
        }

        public class Part
        {
            
            /// <summary>
            ///     Title of this part, used as column title.
            /// </summary>
            public string Title
            {
                set { this.title = value; }
                get { return this.title; }
            }
            private string title;


            /// <summary>
            ///     The text before regular expression.
            /// </summary>
            public string PreText
            {
                set { this.preText = value; }
                get { return this.preText; }
            }
            private string preText;

            
            /// <summary>
            ///     The regex of this part.
            /// </summary>
            public string RegEx
            {
                set { this.regEx = value; }
                get { return this.regEx; }
            }
            private string regEx;


            /// <summary>
            ///     The standard width, used when creating a connected column.
            /// </summary>
            public int StandardWidth
            {
                set { this.standardWidth = value; }
                get { return this.standardWidth; }
            }
            private int standardWidth;

            /// <summary>
            ///     The format of a time or date content.
            /// </summary>
            public string Format
            {
                set { this.format = value; }
                get { return this.format; }
            }
            private string format;
        }
    }
}
